// V5RdWorksOptionDlg.cpp : Implementation of CAssemblyTreeDlg

#include "stdafx.h"
#include "AssemblyTreeDlg.h"
#include "V5RdWorks.h"
//#include <afxcmn.h>
//#include "afxwin.h"

// CAssemblyTreeDlg
#define IMAGEWIDTH		18
#define IMAGEGROWBY		3
#define IMAGE			0
#define IMAGESELECT		1
#define IMAGEVALTEXT	2
#define IMAGEVALBIN		3
#define IMAGECOUNT		3

enum ContentType
{
	Replacement = 0,
	DefaultVal = 1,
	NamedVal = 2,
	Key = 3,
	RootKey = 4
};

void CAssemblyTreeDlg::CreatePopup()
{
	m_hRightMenu = NULL;
	// If you are SDK programmer,
	// _Module.GetResourceInstance() equivalent as AfxGetInstanceHandle
	m_hLoadMenu = LoadMenu(_AtlBaseModule.GetResourceInstance(),
		MAKEINTRESOURCE(IDR_RIGHTMENU));
	if (m_hLoadMenu == NULL)
	{
		ATLTRACE(_T("Failed to load right click menu\n"));
		_ASSERTE(FALSE);
		return;
	}

	m_hRightMenu = GetSubMenu(m_hLoadMenu, 0);
	if (m_hRightMenu == NULL)
	{
		ATLTRACE(_T("Failed to load Popup menu\n"));
		_ASSERTE(FALSE);
		return;
	}
}

void CAssemblyTreeDlg::AddChildNode(EBomTreeNode** ppChildNodes,
	int iChildNum, HTREEITEM& rootItem)
{
	int i = 0;
	EBomTreeNode* curNode = NULL;
	for (i = 0; i < iChildNum; ++i)
	{
		curNode = ppChildNodes[i];
		if (curNode == NULL)
		{
			continue;
		}
		int iNum = curNode->GetSize();
		TreeCtrlItem.hParent = rootItem;
		TreeCtrlItem.hInsertAfter = TVI_LAST;
		TreeCtrlItem.item.mask = TVIF_TEXT | TVIF_IMAGE |TVIF_PARAM | TVIF_SELECTEDIMAGE;
		TreeCtrlItem.item.pszText = (LPWSTR)curNode->GetNodeName();
		if (iNum > 0)
		{
			TreeCtrlItem.item.iImage = 1;
			TreeCtrlItem.item.iSelectedImage = 1;
		}
		else
		{
			TreeCtrlItem.item.iImage = 2;
			TreeCtrlItem.item.iSelectedImage = 2;
		}
		TreeCtrlItem.item.lParam = (LPARAM)curNode;
		HTREEITEM curItem = reinterpret_cast<HTREEITEM>(m_ctlSysTreeView32.SendMessage(TVM_INSERTITEM,
			0, reinterpret_cast<LPARAM>(&TreeCtrlItem)));
		if (iNum > 0)
		{
			AddChildNode(curNode->GetChilds(),
				iNum, curItem);
			TreeView_Expand(m_ctlSysTreeView32.m_hWnd, curItem, TVE_EXPAND);
		}
	}
}

void CAssemblyTreeDlg::DrawAssemblyTree()
{
	TreeCtrlItem.hParent = TVI_ROOT;
	TreeCtrlItem.hInsertAfter = TVI_LAST;
	TreeCtrlItem.item.mask = TVIF_TEXT | TVIF_IMAGE |TVIF_PARAM | TVIF_SELECTEDIMAGE;
	TreeCtrlItem.item.pszText = (LPWSTR)m_treeRootNode->GetNodeName();
	TreeCtrlItem.item.iImage = 0;
	TreeCtrlItem.item.iSelectedImage = 0;
	TreeCtrlItem.item.lParam = (LPARAM)m_treeRootNode;
	m_hAtRoot = reinterpret_cast<HTREEITEM>(m_ctlSysTreeView32.SendMessage(TVM_INSERTITEM , 
		0, reinterpret_cast<LPARAM>(&TreeCtrlItem)));

	int iChildNodeNum = m_treeRootNode->GetSize();
	EBomTreeNode** ppChildNodes = m_treeRootNode->GetChilds();
	AddChildNode(ppChildNodes, iChildNodeNum, m_hAtRoot);

	TreeView_SetImageList(m_ctlSysTreeView32.m_hWnd, m_hImageList, TVSIL_NORMAL);
	TreeView_Expand(m_ctlSysTreeView32.m_hWnd, m_hAtRoot, TVE_EXPAND);
}

LRESULT CAssemblyTreeDlg::OnInitDialog(UINT uMsg, WPARAM wParam,
	LPARAM lParam, BOOL& bHandled)
{
	LONG lRecCount;
	RECT rc;
	GetWindowRect(&rc);
	rc.right -= rc.left+3;
	rc.top = rc.left = 0;
	rc.bottom -= 250;
	InflateRect(&rc, -2, -2);
	InitCommonControls();
	m_ctlSysTreeView32.Create(m_hWnd, rc, _T("SysTreeView32"), 
		WS_VISIBLE | WS_CHILD | TVS_LINESATROOT | TVS_HASLINES | 
		TVS_HASBUTTONS | TVS_FULLROWSELECT | TVS_SHOWSELALWAYS, WS_EX_CLIENTEDGE, IDC_TREE);

	//Modify the tree icon to 24bit
	//m_hImageList = ImageList_LoadBitmap(_AtlBaseModule.GetModuleInstance(),
	//	MAKEINTRESOURCE(IDR_IMAGELIST), IMAGEWIDTH, IMAGEGROWBY, ILC_COLOR24/*OleTranslateColor(m_oleFourColor, NULL, &m_clrFourColor)*/);
	CreatePopup();
	HINSTANCE hInstance = _AtlBaseModule.GetModuleInstance();
	HBITMAP hImage = ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDR_IMAGELIST));
	m_hImageList = ImageList_Create(16, 15, ILC_COLOR24, 9, 1);
	ImageList_Add(m_hImageList, hImage, NULL);
	DeleteObject(hImage);

	_ASSERTE(m_ctlSysTreeView32.m_hWnd != NULL);
	_ASSERTE(m_hImageList != NULL);
	this->DrawAssemblyTree();

	CAxDialogImpl<CAssemblyTreeDlg>::OnInitDialog(uMsg, wParam, lParam, bHandled);

	this->m_ctlSysTreeView32.SetFocus();


	return 1;  // Let the system set the focus
}

LRESULT CAssemblyTreeDlg::OnRButtonDown(UINT uMsg, WPARAM wParam,
	LPARAM lParam, BOOL& bHandled)
{
	HTREEITEM hSel = TreeView_GetSelection(this->m_ctlSysTreeView32.m_hWnd);
	if (hSel == NULL)
	{
		return 0;
	}
	POINT ptClick;
	ptClick.x = LOWORD(lParam);
	ptClick.y = HIWORD(lParam);
	::ClientToScreen(m_ctlSysTreeView32.m_hWnd, &ptClick);
	TrackPopupMenuEx(m_hRightMenu, TPM_VERTICAL, ptClick.x, ptClick.y, m_ctlSysTreeView32.m_hWnd, NULL);
	return 0;
}

LRESULT CAssemblyTreeDlg::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	HTREEITEM hSel = TreeView_GetSelection(this->m_ctlSysTreeView32.m_hWnd);
	if (hSel == NULL)
	{
		EndDialog(wID);
		return 0;
	}

	LPARAM lParam;
	GetLParam(hSel, lParam);
	EBomTreeNode* curNode = (EBomTreeNode*)lParam;
	bool bOpenPart = false;
	if (curNode->GetSize() == 0)
	{
		bOpenPart = true;
	}
	m_pV5Reader->CreateDocuments( curNode, 0 );
	m_pV5Reader->BuildAssembly( NULL, curNode->GetSize(), curNode,
		0, bOpenPart);


	EndDialog(wID);
	return 0;
}

LRESULT CAssemblyTreeDlg::OnOpen(WORD wNotifyCode, WORD wID, HWND hWndCtlr, BOOL& bHandled)
{
	HTREEITEM hSel = TreeView_GetSelection(this->m_ctlSysTreeView32.m_hWnd);
	if (hSel == NULL)
	{
		return 0;
	}
	EndDialog(0);
	LPARAM lParam;
	GetLParam(hSel, lParam);
	EBomTreeNode* curNode = (EBomTreeNode*)lParam;
	bool bOpenPart = false;
	if (curNode->GetSize() == 0)
	{
		bOpenPart = true;
	}
	m_pV5Reader->CreateDocuments( curNode, 0 );
	m_pV5Reader->BuildAssembly( NULL, curNode->GetSize(), curNode,
		0, bOpenPart);

	return 0;
}

LRESULT CAssemblyTreeDlg::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	if(m_ctlSysTreeView32)
	{
		RECT rect = { 0, 0, LOWORD(lParam), HIWORD(lParam) };
		m_ctlSysTreeView32.MoveWindow(&rect);
	}

	return 0;
}
